package com.example.administrator.managesystem.presenter.firstpage;
import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.example.administrator.managesystem.bean.CollectBean;
import com.example.administrator.managesystem.bean.MyShareCircleBean;
import com.example.administrator.managesystem.bean.RepostBean;
import com.example.administrator.managesystem.bean.WikiDetailBean;
import com.example.administrator.managesystem.bean.WikiListBean;
import com.example.administrator.managesystem.fragment.firstpage.WikiFragment;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.FirstPageView;
import com.example.administrator.managesystem.ui.WikiView;
import com.example.administrator.managesystem.utils.LogUtil;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by TR-AND2 on 2017/12/14.
 */

public class FirstPagePresenter {
    private WikiView wikiView;
    private FirstPageView firstPageView;

    public FirstPagePresenter(FirstPageView firstPageView) {
        this.firstPageView = firstPageView;
    }

    public FirstPagePresenter(WikiView wikiView) {
        this.wikiView = wikiView;
    }

    public void myShareCircle(String user_id, String token, final int page, int pageSize, final int type, final  Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).MyShareRequest(user_id,token,page,pageSize)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<MyShareCircleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(MyShareCircleBean myShareCircleBean) {
                       if(myShareCircleBean.getStatus()==200){
                           Log.e("msg","myShareCircleBean:"+myShareCircleBean.getData().toString());
                           firstPageView.updateUI(myShareCircleBean.getData(),type);

                       }else{
                           ToastUtils.showToast(context,myShareCircleBean.getMsg());
                       }

                    }

                });


    }

    public void collectRequest(String user_id, String breeding_id, String token, final int position, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).CollectCircleRequest(user_id,breeding_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<CollectBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(CollectBean collectBean) {
                      if(collectBean.getStatus()==200){
                          ToastUtils.showToast(context,collectBean.getData());
                          firstPageView.showSuccess(position);
                      }
                    }

                });
    }
    public void repostRequest(String user_id, String breeding_id, String token, final int type, final  Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).shareWikiRequest(user_id,breeding_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<RepostBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(RepostBean repostBean) {
                        if(repostBean.getStatus()==200){
                            firstPageView.repost("",type);
                        }
                    }

                });
    }
    public void wikiListRequest(int pageSize, String last_time, final int type, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).wikiRequest(pageSize,last_time)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<WikiListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(WikiListBean wikiListBean) {
                     if(wikiListBean.getStatus()==200){
                        wikiView.showWikiList(wikiListBean.getData(),type);
                     }
                    }

                });
    }
    public void wikiDetailRequest(String user_id,String wiki_id,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).wikiDetailRequest(wiki_id,user_id)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<WikiDetailBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(WikiDetailBean wikiDetailBean) {
                       if(wikiDetailBean.getStatus()==200){
                           wikiView.showWikiDetail(wikiDetailBean.getData());
                       }
                    }

                });
    }

    public void collectArticleRequest(String user_id, String wiki_id,String token,final  Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).CollectWikiRequest(user_id,wiki_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<CollectBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(CollectBean collectBean) {
                        if(collectBean.getStatus()==200){
                            ToastUtils.showToast(context,collectBean.getData());
                            wikiView.updateCollectUI();
                        }
                    }

                });
    }
    public void repostArticleRequest(String user_id, String breeding_id, String token, final int type, final  Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).shareWikiRequest(user_id,breeding_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<RepostBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(RepostBean repostBean) {
                        if(repostBean.getStatus()==200){
                            wikiView.updateRepost(type);
                        }
                    }

                });
    }
}
